package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.exception.CustomException;
import com.itheima.service.ShoppingCartService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ShoppingCartDao shoppingCartDao;


    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart cart){
        //1.调用service处理
        //1.1：判断购物车cart对象中是否有数据  非空判断：提示 "请选择商品添加到购物车！"
        if(cart==null){
            throw new CustomException("请选择商品添加到购物车！");
        }

        //1.2：获取当前登录用户id
        Long userId = BaseContext.getCurrentId();

        //创建查询条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);

        //1.3：判断要添加的商品是菜品还是套餐
        //购物车中 如果该用户下面添加的菜品口味不一致 表示一件新的商品
        if(cart.getDishId()!=null){
            wrapper.eq(ShoppingCart::getDishId,cart.getDishId());
            wrapper.eq(cart.getDishFlavor()!=null,ShoppingCart::getDishFlavor,cart.getDishFlavor());   //有些菜品有口味，有些没有
        }else{
            wrapper.eq(ShoppingCart::getSetmealId,cart.getSetmealId());
        }

        //1.4：判断当前购物车中在该用户下面是否已经存在相同的商品  根据用户id和菜品|套餐id去进行查询  存在：+1  不存在：商品数量设置为1
        ShoppingCart one = shoppingCartService.getOne(wrapper); //从数据库中查询出登录用户的购物车商品信息
        if(one==null){
            //说明数据库中不存在该商品  是一件新的商品添加到数据库，将cart赋值给one 并且设置关联的用户id和商品数量以及添加时间
            one = cart;
            one.setUserId(userId);
            one.setNumber(1);
            one.setCreateTime(LocalDateTime.now());
            //保存商品信息到购物车 新增
            //shoppingCartService.save(one);
        }else{
            //不是新的商品  数量+1
            one.setNumber(one.getNumber()+1);
            //修改商品信息保存到购物车 修改
            //shoppingCartService.updateById(one);
        }
        //1.5：添加商品到购物车
        shoppingCartService.saveOrUpdate(one); //保存修改  id为null：新增  id不为null：修改

        //2.响应数据
        return R.success(one);
    }

    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //0.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();

        //1.调用service处理
        //1.1：创建wrapper对象
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //1.2：设置查询条件 userId
        wrapper.eq(ShoppingCart::getUserId,userId);
        //1.3：调用list方法传入wrapper完成查询
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        //2.响应数据
        return R.success(list);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        //1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();

        //2.调用service处理 根据用户id清空购物车中当前用户的商品数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        boolean flag = shoppingCartService.remove(wrapper);

        //3.响应数据
        return flag?R.success("清空成功！"):R.error("清空失败！");
    }

    /**
     * 购物车减少商品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @Transactional
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //代表数量减少的是菜品数量
        if (dishId != null){
            //通过dishId查出购物车对象
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart cart1 = shoppingCartService.getOne(queryWrapper);
            cart1.setNumber(cart1.getNumber()-1);
            //对数据进行更新操作
            shoppingCartService.updateById(cart1);
            return R.success(cart1);
        }
        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId != null){
            //代表是套餐数量减少
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart cart2 = shoppingCartService.getOne(queryWrapper);
            cart2.setNumber(cart2.getNumber()-1);
            //对数据进行更新操作
            shoppingCartService.updateById(cart2);
            return R.success(cart2);
        }
        return R.error("操作有误");
    }
}
